<?php
declare(strict_types=1);

namespace LowCode\Naive;

use LowCode\Naive;
use LowCode\Node;
use LowCode\Traits\Events\HasInputEvents;
use LowCode\Traits\HasDeprecatedChildren;
use LowCode\Traits\Props\HasAutofocus;
use LowCode\Traits\Props\HasClearable;
use LowCode\Traits\Props\HasDefaultValue;
use LowCode\Traits\Props\HasDisabled;
use LowCode\Traits\Props\HasLoading;
use LowCode\Traits\Props\HasModel;
use LowCode\Traits\Props\HasReadonly;
use LowCode\Traits\Props\HasRound;
use LowCode\Traits\Props\HasTinySize;
use LowCode\Traits\Props\HasValue;
use LowCode\Traits\Slots\HasSlotPrefix;
use LowCode\Traits\Slots\HasSlotSuffix;

/**
 * Input组件
 * @method static Input make(string $type = 'text', mixed $value = null) Input组件
 */
class Input extends Naive
{
    use HasLoading, HasDisabled, HasRound, HasClearable, HasReadonly, HasDefaultValue, HasValue, HasModel;
    use HasAutofocus, HasTinySize;
    use HasSlotPrefix, HasSlotSuffix;
    use HasInputEvents;
    use HasDeprecatedChildren;

    public function __construct(string $type = 'text', mixed $value = null)
    {
        parent::__construct();
        $this->type($type);
        $this->clearable();
        $value && $this->defaultValue($value);
    }


    /**
     * 设置输入框类型
     * @param string $type 输入框类型 'text' | 'password' | 'textarea'
     * @return $this
     */
    public function type(string $type = 'text' | 'password' | 'textarea'): static
    {
        return $this->prop("type",$type);
    }

    /**
     * 设置输入框验证状态
     * @param string $status 输入框验证状态 'success' | 'warning' | 'error'
     * @return $this
     */
    public function status(string $status = 'success' | 'warning' | 'error'): static
    {
        return $this->prop("status",$status);
    }

    /**
     * 是否输入成对的值
     * @param string|bool $pair
     * @return $this
     */
    public function pair(string|bool $pair = true): static
    {
        return $this->prop("pair",$pair);
    }

    /**
     * 文本输入的占位符 如果 pair 是 true，placeholder是一个数组
     * @param string|array $placeholder
     * @return $this
     */
    public function placeholder(string|array $placeholder = ''): static
    {
        return $this->prop("placeholder",$placeholder);
    }

    /**
     * 成对输入框中间的分隔符
     * @param string $separator
     * @return $this
     */
    public function separator(string $separator = ''): static
    {
        return $this->prop("separator",$separator);
    }

    /**
     * 是否显示字数统计
     * @param string|bool $showCount
     * @return $this
     */
    public function showCount(string|bool $showCount = true): static
    {
        return $this->prop("show-count",$showCount);
    }

    /**
     * 是否显示字数统计
     * @param string $renderCount (props: { value: string }) => void
     * @param string|null $id
     * @return $this
     */
    public function renderCount(string $renderCount, string $id = null): static
    {
        return $this->jsFunction("render-count",$renderCount, $id);
    }

    /**
     * 校验当前的输入是否合法，如果返回 false 输入框便不会响应此次的输入
     * @param string $allowCount (value: string) => false
     * @param string|null $id
     * @return $this
     */
    public function allowCount(string $allowCount, string $id = null): static
    {
        return $this->jsFunction("allow-count",$allowCount, $id);
    }

    /**
     * 最小输入长度
     * @param int $minlength
     * @return $this
     */
    public function minlength(int $minlength): static
    {
        return $this->prop("minlength", $minlength);
    }

    /**
     * 最大输入长度
     * @param int $maxlength
     * @return $this
     */
    public function maxlength(int $maxlength): static
    {
        return $this->prop("maxlength",$maxlength);
    }

    /**
     * 输入长度范围
     * @param string|int $minlength
     * @param string|int $maxlength
     * @return $this
     */
    public function length(string|int $minlength = '', string|int $maxlength = ''): static
    {
        $this->minlength($minlength);
        $this->maxlength($maxlength);
        return $this;
    }

    /**
     * 是否被动激活输入框
     * @param string|bool $passivelyActivated
     * @return $this
     */
    public function passivelyActivated(string|bool $passivelyActivated = true): static
    {
        return $this->prop("passively-activated",$passivelyActivated);
    }


    /**
     * Input 组件内部 input 元素的属性，对 pair 类型不生效，在这里查看原生属性。注意：input-props 不会覆盖内部 input 元素的已经存在的属性（除了 type）
     * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input
     * @param array|string $inputProps
     * @return $this
     */
    public function inputProps(array|string $inputProps = []): static
    {
        return $this->prop("input-props",$inputProps);
    }


    /**
     * 自定义清除图标
     * @param Node $node
     * @param array $params
     * @return $this
     */
    public function slotClearIcon(Node $node, array $params = []): static
    {
        return $this->addSlot($node,"clear-icon", $params);
    }

    /**
     * 字数统计 (props: { value: string })
     * @param Node $node
     * @param array $params
     * @return $this
     */
    public function slotCount(Node $node, array $params = []): static
    {
        return $this->addSlot($node,"count", $params);
    }

}
